package com.hxzy.dao.impl;

import com.hxzy.dao.FilmDao;
import com.hxzy.util.DbHelper;
import entity.Film;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.util.Optional;

public class FilmDaoImpl extends DbHelper implements FilmDao {
    @Override
    public int insert(Film film) {
        String sql="insert into film (film_name,show_date,film_time,hot) values(?,?,?,?)";
        Object[] objects={
               film.getFilmName(),
               film.getFilmDate(),
               film.getFilmTime(),
                film.getFilmHot()

        };
        return super.executeUpdate(sql,objects);

    }

    @Override
    public int updateByPrimaryKey(Film film) {
        String sql="update  film set film_name=?,show_date=?,film_time=?,hot=? where film_id=?";
        Object[] objects={
                film.getFilmName(),
                film.getFilmDate(),
                film.getFilmTime(),
                film.getFilmHot(),
                film.getFilmId()

        };
        return super.executeUpdate(sql,objects);

    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        String sql="delete from film where film_id=?";
        return super.executeUpdate(sql,id);
    }

    @Override
    public Optional<Film> selectByPrimaryKey(Integer id) {
        String sql="select film_id as filmId, film_name as filmName,show_date as filmDate ,film_time as filmTime ,hot as filmHot from film where film_id=?";
        BeanHandler<Film> filmBeanHandler = new BeanHandler<>(Film.class);
        return super.executeQuery(sql,filmBeanHandler,id);
    }
}
